package com.apiserver.consumer.console.controller.system;


import com.apiserver.consumer.console.api.system.UsersInterface;
import com.apiserver.kernel.result.JsonResult;
import com.apiserver.kernel.result.StatusCode;
import com.apiserver.kernel.utils.Constant;
import com.apiserver.kernel.utils.TokenGenerator;
import com.apiserver.model.system.entity.SUsers;
import com.apiserver.model.system.response.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author feizhou
 * @Description 测试
 * @Date 下午 10:06 2019/10/14 0014
 * @Param
 * @return
 **/

@Api(description = "用户信息管理", tags = "s-UsersController", basePath = "/users")
@RestController
@RequestMapping(value = "/v1/users", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UsersController {

    public static final String token = "token";


    @Autowired
    UsersInterface usersInterface;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据用户ID，查询用户的详细信息", notes = "返回单个用户的信息,用户的详细信息")
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public JsonResult<SUsers> get(@RequestParam(value = "id") Long id) {
        return JsonResult.getSuccessResult(usersInterface.get(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据查询的参数，查询用户的列表信息", notes = "返回用户的列表信息。")
    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public JsonResult<PageInfo<SUsers>> getAll(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                               @RequestParam(value = "account", required = false) String account,
                                               @RequestParam(value = "mobile", required = false) String mobile,
                                               @RequestParam(value = "email", required = false) String email,
                                               @RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "loginType", required = false) Integer loginType) {

        return JsonResult.getSuccessResult(usersInterface.getAll(pageNum, pageSize, account, mobile, email, status, loginType));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存用户的信息", notes = "保存用户的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<Map<String, Object>> save(@RequestBody @Valid SUsers users) {
        Map<String, Object> resultMap = usersInterface.save(users);
        if (MapUtils.isNotEmpty(resultMap)) {
            return JsonResult.getFailureResult(StatusCode.OK, resultMap);
        }
        return JsonResult.getSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "修改用户的信息", notes = "修改用户的信息")
    @RequestMapping(value = "edite", method = RequestMethod.POST)
    public JsonResult<Map<String, Object>> edite(ServletRequest request, @RequestBody SUsers usersEntity) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
        if (null != authorization) {
            Long userId = TokenGenerator.getUserId(authorization);
            usersEntity.setModifyId(String.valueOf(userId));
        }
        Map<String, Object> resultMap = usersInterface.edite(usersEntity);
        if (MapUtils.isNotEmpty(resultMap)) {
            return JsonResult.getFailureResult(StatusCode.OK, resultMap);
        }
        return JsonResult.getSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除用户的信息", notes = "根据用户的ID,删除数据")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public JsonResult<Map<String, Object>> delete(@RequestBody Long id) {
        Map<String, Object> delete = usersInterface.delete(id);

        return JsonResult.getSuccessResult(delete);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统用户唯一ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "用户系统账号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "系统密码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "chineseName", value = "中文名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "用户邮箱", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lastDate", value = "最后登录时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "birthday", value = "出生年月", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "nationality", value = "国籍", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cities", value = "城市", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "居住地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "idType", value = "证件类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "idNumber", value = "证件号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "education", value = "学历", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "household", value = "户籍地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ethnic", value = "民族", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "blood", value = "血型", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "career", value = "职业", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "激活状态", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "loginType", value = "登陆类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "skin", value = "用户皮肤", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "modifyId", value = "修改人", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "头像路径", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "registerType", value = "注册类型", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "registerCode", value = "注册码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qqNumber", value = "qq账号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "wechat", value = "微信号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "weibo", value = "微博地址", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdWechat", value = "微信的openID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdQq", value = "qq注册生成id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openIdWeibo", value = "微博注册生成ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })

    @ApiOperation(value = "列表", notes = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public JsonResult<List<SUsers>> list(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "pwd", required = false) String pwd,
            @RequestParam(value = "chineseName", required = false) String chineseName,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "lastDate", required = false) Date lastDate,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "birthday", required = false) Date birthday,
            @RequestParam(value = "nationality", required = false) String nationality,
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "cities", required = false) String cities,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "idType", required = false) Integer idType,
            @RequestParam(value = "idNumber", required = false) String idNumber,
            @RequestParam(value = "education", required = false) String education,
            @RequestParam(value = "age", required = false) Integer age,
            @RequestParam(value = "household", required = false) String household,
            @RequestParam(value = "ethnic", required = false) String ethnic,
            @RequestParam(value = "blood", required = false) String blood,
            @RequestParam(value = "career", required = false) String career,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "loginType", required = false) Integer loginType,
            @RequestParam(value = "skin", required = false) String skin,
            @RequestParam(value = "modifyId", required = false) String modifyId,
            @RequestParam(value = "roleId", required = false) Long roleId,
            @RequestParam(value = "imageUrl", required = false) String imageUrl,
            @RequestParam(value = "userType", required = false) Integer userType,
            @RequestParam(value = "registerType", required = false) Integer registerType,
            @RequestParam(value = "registerCode", required = false) String registerCode,
            @RequestParam(value = "qqNumber", required = false) String qqNumber,
            @RequestParam(value = "wechat", required = false) String wechat,
            @RequestParam(value = "weibo", required = false) String weibo,
            @RequestParam(value = "openIdWechat", required = false) String openIdWechat,
            @RequestParam(value = "openIdQq", required = false) String openIdQq,
            @RequestParam(value = "openIdWeibo", required = false) String openIdWeibo
    ) {
        return JsonResult.getSuccessResult(usersInterface.list(id, account, pwd, chineseName, nickname, email, mobile, lastDate, sex, birthday, nationality, province, cities, address, idType, idNumber, education, age, household, ethnic, blood, career, status, loginType, skin, modifyId, roleId, imageUrl, userType, registerType, registerCode, qqNumber, wechat, weibo, openIdWechat, openIdQq, openIdWeibo));
    }

}
